package org.assistant.csp.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.assistant.common.jdbc.core.RowMapper;
import org.assistant.common.jdbc.dao.BasicDao;
import org.assistant.csp.data.Menu;

public class MenuDaoImpl extends BasicDao implements MenuDao {
	private class MenuRowMapper implements RowMapper<Menu> {
		public Menu mapRow(ResultSet rs, int rowNum) throws SQLException {
			Menu menu = new Menu();

			menu.setId(rs.getInt("menu_id_"));
			menu.setName(rs.getString("menu_name_"));
			menu.setUrl(rs.getString("url_"));
			menu.setLevel(rs.getInt("level_"));
			menu.setParentId(rs.getInt("parent_id_"));
			menu.setStatus(rs.getBoolean("status_"));
			menu.setExplain(rs.getString("explain_"));
			menu.setLft(rs.getInt("lft_"));
			menu.setRgt(rs.getInt("rgt_"));

			return menu;
		}
	}

	/**
	 * 获取所有菜单
	 * 
	 * @return
	 * @throws SQLException
	 */
	public List<Menu> getAllMenus() throws SQLException {
		String sql = "SELECT a.menu_id_,a.menu_name_,a.url_,a.level_,a.parent_id_,a.status_,a.explain_,a.lft_,a.rgt_ FROM common_menu a order by a.lft_";

		List<Menu> menuList = getJdbcTemplate().queryForList(
			sql, new MenuRowMapper());

		return menuList;
	}
}
